package com.app.lxx.pregnancyberth.base;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.app.lxx.pregnancyberth.Mvp.Presenter.BasePresenter;
import com.app.lxx.pregnancyberth.Mvp.View.BaseView;
import com.app.lxx.pregnancyberth.widge.SharedPreferencesUtils;
import com.app.lxx.pregnancyberth.widge.UtilsManage;
import com.jaeger.library.StatusBarUtil;


/**
 * Created by Lxx on 2018/4/9.
 * Details：BaseFragment
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {
    private P p;
    protected Context context;
    private Toast toast;
    protected SharedPreferencesUtils shareference;
    protected UrlManage urlManage;
    protected UtilsManage utilsManage;
    protected LocalBroadcastUtils localBroadcastUtils;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.context = activity;
    }

    public P getP() {
        if (p == null) {
            p = createP();
        }
        return p;
    }

    protected abstract P createP();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (p == null) {
            p = createP();

        }
        if (p != null) {
            p.attchView(this);
        }
    }

    private View Contentvew;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(setview(), container, false);
        return inflate;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Contentvew = view;
        shareference = new SharedPreferencesUtils(context);
        urlManage = new UrlManage();
        urlManage.APP_URL=UrlManage.APP_URL;
        urlManage.imageUrl=UrlManage.imageUrl;
        utilsManage = new UtilsManage(context);
        localBroadcastUtils = new LocalBroadcastUtils(context);
        initview();
    }

    public View findviewById(@IdRes int id) {
        if (Contentvew != null) {
            return Contentvew.findViewById(id);
        }
        return null;
    }

    protected abstract int setview();

    protected abstract void initview();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (p != null) {
            p.dettchView();
        }
        if (toast != null) {
            toast.cancel();
        }

        //根据 Tag 取消请求
        // OkGo.getInstance().cancelTag(this);
    }

    @Override
    public void showToast(String msg) {
        if (toast == null) {
            toast = Toast.makeText(context, msg, Toast.LENGTH_LONG);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public void setStatusBarColor(Activity activity, String s) {
        StatusBarUtil.setColor(activity, Color.parseColor(s), 0);
    }

}
